#include "menu.h"
#include <QGraphicsProxyWidget>
#include <QApplication>
#include<QGraphicsView>

Menu::Menu(QObject* parent) : QGraphicsScene(parent) {
   //background=new QGraphicsPixmapItem();
    originalBg= QPixmap(":/images/menubackground.png");
    if(originalBg.isNull()) {
        qWarning() << "Failed to load menu background!";
        originalBg = QPixmap(800, 600);
        originalBg.fill(QColor(40, 40, 40));
    }
    background=new QGraphicsPixmapItem(originalBg);
    background->setZValue(-1);
    addItem(background);
    setupUI();
    resizeBackground(800,600);
}


void Menu::resizeBackground(int width, int height) {
    // 保持宽高比缩放
    QPixmap scaled = originalBg.scaled(
        width, height,
        Qt::KeepAspectRatioByExpanding,
        Qt::SmoothTransformation
        );

    // 居中裁剪
    scaled = scaled.copy(
        (scaled.width() - width) / 2,
        (scaled.height() - height) / 2,
        width,
        height
        );

    background->setPixmap(scaled);
    setSceneRect(0, 0, width, height);
}


void Menu::setupUI() {


    // 创建开始按钮

    startBtn = new QPushButton();
    startBtn->setText("▶ 开始游戏");
    startBtn->setStyleSheet(R"(
        QPushButton {
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                stop:0 #4CAF50, stop:1 #45a049);
            border: 2px solid #2E7D32;
            border-radius: 25px;
            color: white;
            padding: 20px 40px;
            font: bold 24px;
            min-width: 300px;
        }
        QPushButton:hover {
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                stop:0 #66BB6A, stop:1 #4CAF50);
        }
        QPushButton:pressed {
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                stop:0 #388E3C, stop:1 #2E7D32);
        }
    )");






    exitBtn = new QPushButton();
    exitBtn->setText("⏹ 退出游戏");
    exitBtn->setStyleSheet(R"(
        QPushButton {
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                stop:0 #EF5350, stop:1 #D32F2F);
            border: 2px solid #B71C1C;
            border-radius: 25px;
            color: white;
            padding: 20px 40px;
            font: bold 24px;
            min-width: 300px;
        }
        QPushButton:hover {
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                stop:0 #EF9A9A, stop:1 #EF5350);
        }
        QPushButton:pressed {
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                stop:0 #C62828, stop:1 #B71C1C);
        }
    )");


    // 创建玩法说明按钮
    showInstructionsBtn = new QPushButton();
    showInstructionsBtn->setText("📚 玩法说明");
    showInstructionsBtn->setStyleSheet(R"(
        QPushButton {
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                stop:0 #2196F3, stop:1 #1976D2);
            border: 2px solid #0D47A1;
            border-radius: 25px;
            color: white;
            padding: 20px 40px;
            font: bold 24px;
            min-width: 300px;
        }
        QPushButton:hover {
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                stop:0 #64B5F6, stop:1 #2196F3);
        }
        QPushButton:pressed {
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                stop:0 #1565C0, stop:1 #0D47A1);
        }
    )");

    // 添加到场景
    QGraphicsProxyWidget* startProxy = addWidget(startBtn);
    QGraphicsProxyWidget* exitProxy = addWidget(exitBtn);
    QGraphicsProxyWidget* instructionsProxy = addWidget(showInstructionsBtn);

    // 居中显示
    startProxy->setPos(width()/2 - startBtn->width()/2, height()/2 - 120);
    exitProxy->setPos(width()/2 - exitBtn->width()/2, height()/2 + 60);
    instructionsProxy->setPos(width()/2 - showInstructionsBtn->width()/2, height()/2 - 30);

    startProxy->setZValue(1);
    exitProxy->setZValue(1);
    instructionsProxy->setZValue(1);

    //初始化说明窗口
    instructionsWindow=new instructionswindow();
    instructionsWindow->hide();
    // 连接信号槽
    connect(startBtn, &QPushButton::clicked, this, &Menu::onStartClicked);
    connect(exitBtn, &QPushButton::clicked, this, &Menu::onExitClicked);
    connect(showInstructionsBtn, &QPushButton::clicked, this, &Menu::onshowInstructionsClicked);

    connect(instructionsWindow, &instructionswindow::backToMenuRequested, [this]() {
        showInstructionsBtn->show();
        instructionsWindow->hide();
    });
}

void Menu::onStartClicked() {
    emit startGame();
}

void Menu::onExitClicked() {
    emit exitGame();
}

void Menu::onshowInstructionsClicked() {
    showInstructionsBtn->hide();
    instructionsWindow->show();
}
